//
//  TXTradePasswordView.m
//  TF
//
//  Created by --- on 16/1/3.
//  Copyright © 2016年 吴天祥. All rights reserved.
//

#import "TXTradePasswordView.h"
#import "CALayer+Anim.h"
#import "UIView+MBLayout.h"
@implementation TXTradePasswordView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];// 先调用父类的initWithFrame方法
    if (self)
    {
        ///  TF
        _TF = [[UITextField alloc]init];
        _TF.frame = CGRectMake(0, 0, 0, 0);
        _TF.delegate = self;
        _TF.keyboardType=UIKeyboardTypeNumberPad;
        [_TF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_TF];
        
        ///  假的输入框
        _view_box = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth, 0, boxWidth, boxWidth)];
//        [_view_box.layer setBorderWidth:1.0];
//        _view_box.layer.cornerRadius = kCorner;
//        _view_box.layer.borderColor = kLineColor;
        [_view_box setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box];
        _view_box2 = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth +  kLineWith+boxWidth*1, _view_box.frame.origin.y, boxWidth, boxWidth)];
        [_view_box2 setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box2];
        _view_box3 = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth + kLineWith * 2+boxWidth*2, _view_box.frame.origin.y, boxWidth, boxWidth)];
        [_view_box3 setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box3];
        _view_box4 = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth + kLineWith * 3+boxWidth*3, _view_box.frame.origin.y, boxWidth, boxWidth)];
        [_view_box4 setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box4];
        _view_box5 = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth + kLineWith * 4 + boxWidth*4, _view_box.frame.origin.y, boxWidth, boxWidth)];
        [_view_box5 setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box5];
        _view_box6 = [[UIView alloc]initWithFrame:CGRectMake(kBothSidesWidth + kLineWith * 5 + boxWidth*5, _view_box.frame.origin.y, boxWidth, boxWidth)];
        [_view_box6 setLineWithColor:WB_HEX_COLOR(0x333333)];
        [self addSubview:_view_box6];
        
        ///   密码点
        _lable_point = [[UILabel alloc]init];
        _lable_point.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point.layer setCornerRadius:5];
        [_lable_point.layer setMasksToBounds:YES];
        _lable_point.backgroundColor = [UIColor blackColor];
        [_view_box addSubview:_lable_point];
        
        _lable_point2 = [[UILabel alloc]init];
        _lable_point2.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point2.layer setCornerRadius:5];
        [_lable_point2.layer setMasksToBounds:YES];
        _lable_point2.backgroundColor = [UIColor blackColor];
        [_view_box2 addSubview:_lable_point2];
        
        
        _lable_point3 = [[UILabel alloc]init];
        _lable_point3.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point3.layer setCornerRadius:5];
        [_lable_point3.layer setMasksToBounds:YES];
        _lable_point3.backgroundColor = [UIColor blackColor];
        [_view_box3 addSubview:_lable_point3];
        
        _lable_point4 = [[UILabel alloc]init];
        _lable_point4.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point4.layer setCornerRadius:5];
        [_lable_point4.layer setMasksToBounds:YES];
        _lable_point4.backgroundColor = [UIColor blackColor];
        [_view_box4 addSubview:_lable_point4];
        
        _lable_point5 = [[UILabel alloc]init];
        _lable_point5.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point5.layer setCornerRadius:5];
        [_lable_point5.layer setMasksToBounds:YES];
        _lable_point5.backgroundColor = [UIColor blackColor];
        [_view_box5 addSubview:_lable_point5];
        
        _lable_point6 = [[UILabel alloc]init];
        _lable_point6.frame = CGRectMake((_view_box.frame.size.width-10)/2, (_view_box.frame.size.width-10)/2, 10, 10);
        [_lable_point6.layer setCornerRadius:5];
        [_lable_point6.layer setMasksToBounds:YES];
        _lable_point6.backgroundColor = [UIColor blackColor];
        [_view_box6 addSubview:_lable_point6];
        
        _lable_point.hidden=YES;
        _lable_point2.hidden=YES;
        _lable_point3.hidden=YES;
        _lable_point4.hidden=YES;
        _lable_point5.hidden=YES;
        _lable_point6.hidden=YES;
        
        ///标题
        _lable_title = [[UILabel alloc]init];
        _lable_title.frame = CGRectMake(0, CGRectGetMaxY(_view_box.frame) + 50, SCREEN_WIDTH, 20);
        _lable_title.textAlignment=1;
        _lable_title.font = [UIFont systemFontOfSize:13];
        _lable_title.textColor = WB_RGBColor(237, 0, 0);
        _lable_title.text = @"请设置六位数交易密码";
        _lable_title.textColor = WB_RGBColor(107, 170, 249);
        [self addSubview:_lable_title];
    }
    return self;
}

- (void)textFieldDidChange:(id)sender
{
    NSString *string =@"123456d890";
    NSString *str1 = [string substringToIndex:5];//截取掉下标5之前的字符串
    NSLog(@"截取的值为：%@",str1);
    NSString *str2 = [string substringFromIndex:3];//截取掉下标3之后的字符串
    NSLog(@"截取的值为：%@",str2);
    
    UITextField *_field = (UITextField *)sender;
    if (_field.text.length > 6) {
        NSString *str = [_field.text substringToIndex:6];
        _field.text = str;
        NSLog(@"发发发ffff：%@",str);
    }
    NSLog(@"发发发：%@",_field.text);
    switch (_field.text.length) {
        case 0:
        {
            _lable_point.hidden=YES;
            _lable_point2.hidden=YES;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 1:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=YES;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 2:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=YES;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 3:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=YES;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 4:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=YES;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 5:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=NO;
            _lable_point6.hidden=YES;
            self.isError = NO;
        }
            break;
        case 6:
        {
            _lable_point.hidden=NO;
            _lable_point2.hidden=NO;
            _lable_point3.hidden=NO;
            _lable_point4.hidden=NO;
            _lable_point5.hidden=NO;
            _lable_point6.hidden=NO;
        }
            break;
            
        default:
            break;
    }
    if (_field.text.length==6)
    {        
        [self.TXTradePasswordDelegate TXTradePasswordView:self WithPasswordString:_field.text];
        
    }
}

- (void)setIsSurePws:(BOOL)isSurePws
{
    if (isSurePws) {
        _lable_title.text = @"请确认六位数交易密码";
    }else{
        _lable_title.text = @"请设置六位数交易密码";
    }
}

- (void)setIsError:(BOOL)isError
{
    if (isError) {
        _lable_title.text = @"两次填写的密码不一致";
        _lable_title.textColor = kRedLineColor;
        [_lable_title.layer shake];
        [_view_box setLineWithColor:kRedLineColor];
        [_view_box2 setLineWithColor:kRedLineColor];
        [_view_box3 setLineWithColor:kRedLineColor];
        [_view_box4 setLineWithColor:kRedLineColor];
        [_view_box5 setLineWithColor:kRedLineColor];
        [_view_box6 setLineWithColor:kRedLineColor];
    }else{
        if (_isSurePws) {
            _lable_title.text = @"请设置六位数交易密码";
        }else{
            _lable_title.text = @"请确认六位数交易密码";
        }
        _lable_title.textColor = WB_RGBColor(107, 170, 249);
        [_view_box setLineWithColor:kLineColor];
        [_view_box2 setLineWithColor:kLineColor];
        [_view_box3 setLineWithColor:kLineColor];
        [_view_box4 setLineWithColor:kLineColor];
        [_view_box5 setLineWithColor:kLineColor];
        [_view_box6 setLineWithColor:kLineColor];
    }
}

@end
